Skip to content

Conversation

@TheRealNeil
Copy link
Contributor

@TheRealNeil TheRealNeil commented Feb 8, 2026

Discard handler return value in exception handling to prevent polluting raw_response

Fixes #305

Summary

  • Add explicit nil return after rescue_with_handler in with_exception_handling to prevent the handler's return value leaking into raw_response

Root cause

When an exception occurs during api_prompt_execute, with_exception_handling returns whatever the rescue handler returns (often a truthy value like a String from broadcast_error). This becomes raw_response in resolve_prompt, which then causes a secondary NoMethodError: undefined method 'usage' for an instance of String — swallowing the real error.

Fix

Return nil explicitly after rescue_with_handler(exception) || raise. This ensures raw_response is always nil when an exception has been handled, preventing the secondary error while still letting the agent's rescue_from chain handle the original exception.

Test plan

  • Trigger a provider exception with a rescue_from handler that returns a truthy value and confirm no secondary NoMethodError on usage
  • Confirm unhandled exceptions still re-raise as expected
  • Confirm rescue_from handlers are still invoked correctly

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses an error-handling edge case in provider execution where a truthy rescue_from handler return value can unintentionally become the provider raw_response, causing follow-on failures (e.g., calling usage on a String) and obscuring the original exception.

Changes:

  • Ensure with_exception_handling discards the rescue handler’s return value by explicitly returning nil after rescue_with_handler(exception) || raise.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +58 to 60
nil # Discard handler return value to prevent polluting raw_response
end

Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes with_exception_handling to always return nil when an exception is handled, which alters the existing contract where the handler’s return value is propagated. There is a current unit test that asserts the handler return value is returned (test/providers/concerns/exception_handler_test.rb:57-66), so this PR will fail CI unless the tests (and any other callers) are updated. If you want to avoid a behavior change in this concern, consider keeping with_exception_handling returning the handler result and instead change the BaseProvider call sites to not assign the method’s return value to raw_response (so raw_response stays nil on exceptions).

Suggested change
nil # Discard handler return value to prevent polluting raw_response
end
end

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

with_exception_handling returns handler's value, polluting raw_response

1 participant